package DAO;

import java.sql.Array;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Vector;

import oracle.net.TNSAddress.Description;

public class TreatmentDao {
  public TreatmentDao() {
  }

  public boolean creaTratamiento(String des, float cost,
                                 int quantity, String obs, Date begin,
                                 Date end, int exanum) {

    Conexion conexion = new Conexion();
    boolean creado = false;
    Connection conex;

    try {
      int treatmentNum;
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ResultSet rsa = null;
      int mayor = -1;
      ps =
          conex.prepareStatement("select max (treatmentnumber)  from treatment");
      rsa = ps.executeQuery();
      if (rsa != null) {
        while (rsa.next()) {
          mayor = rsa.getInt(1);
        }
      } else
        treatmentNum = 1;


      ps =
          conex.prepareStatement("INSERT INTO TREATMENT(TREATMENTNUMBER,DESCRIPTION ,TCOST,QUANTITY,OBSERVATION,BEGINDATE,ENDDATE,EXAMINATIONNUMBER) VALUES (?,?,?,?,?,?,?,?)");
      treatmentNum = mayor + 1;
      ps.setInt(1, treatmentNum);
      ps.setString(2, des);
      ps.setFloat(3, cost);
      ps.setInt(4, quantity);
      ps.setString(5, obs);
      ps.setDate(6, begin);
      ps.setDate(7, end);
      ps.setInt(8, exanum);


      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que creo fueron" +
                         numeroFilas);
      creado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return creado;
  }


  public boolean modificaTreatmentDescripcion(int numtreatment, String desc) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE treatment set description = ?  where treatmentnumber = ?");

      ps.setString(1, desc);

      ps.setInt(2, numtreatment);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaTreatmentCosto(int numtreatment, float cost) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE treatment set tcost = ?  where treatmentnumber = ?");

      ps.setFloat(1, cost);

      ps.setInt(2, numtreatment);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaTreatmentCantidad(int numtreatment, int canti) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;


System.out.println("Estamos en DAO " + numtreatment);
      ps =
          conex.prepareStatement("UPDATE treatment set quantity = ?  where treatmentnumber = ?");

      ps.setInt(1, canti);

      ps.setInt(2, numtreatment);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaTreatmentObserv(int numtreatment, String obs) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE treatment set observation = ?  where treatmentnumber = ?");

      ps.setString(1, obs);

      ps.setInt(2, numtreatment);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaTreatmentDateBeg(int numtreatment, Date begin) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE treatment set begindate = ?  where treatmentnumber = ?");

      ps.setDate(1, begin);

      ps.setInt(2, numtreatment);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaTreatmentDateEnd(int numtreatment, Date end) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE treatment set enddate = ?  where treatmentnumber = ?");

      ps.setDate(1, end);

      ps.setInt(2, numtreatment);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean modificaTreatmentExaNum(int numtreatment, int numEx) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE treatment set examinationnumber = ?  where treatmentnumber = ?");

      ps.setInt(1, numEx);

      ps.setInt(2, numtreatment);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);

      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }

  public boolean eliminarTratamiento(int treatmentNum) {

    Conexion conexion = new Conexion();
    boolean eliminado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ps = conex.prepareStatement("DELETE FROM Treatment where Treatmentnumber = ?");
      ps.setInt(1, treatmentNum);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que borro fueron" + numeroFilas);
      eliminado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return eliminado;
  }

  public Object[][] DatosTratamientoTabla() {
    Conexion conexion = new Conexion();
    Connection conex;
    Object[][] datos = new Object[100][100];

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ps = conex.prepareStatement("select * FROM Treatment ");     
      int i = 0;
      int j = 0;

      rs = ps.executeQuery();
      System.out.println(rs + " es el resultado de la consulta !");
      
      datos[i][j]= "TREATMENT NUMBER";
      j++;
      datos[i][j]="DESCRIPTION";
      j++;
      datos[i][j]="TCOST";
      j++;
      datos[i][j]="QUANTITY";
      j++;
      datos[i][j]="OBSERVATION";
      j++;
      datos[i][j]= "BEGINDATE";
      j++;
      datos[i][j]="END DATE";
      j++;
      datos[i][j]="EXAMINATION NUMBER";
      i++;
      j=0;
      while (rs.next()) {
        datos[i][j] = rs.getInt(1);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getString(2);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getFloat(3);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getInt(4);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getString(5);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getDate(6);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getDate(7);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getInt(8);
        System.out.print(datos[i][j] + " ");
        System.out.println("FIN!! ");
        i++;
        j=0;
      }
      
      rs.close();
      ps.close();
      conex.close();

      
      for (int f = 1; f < 9; f++) {
        for (int g = 0; g < 9; g++) {
          System.out.println("datos");
          System.out.print(datos[f][g]);
        }
      }  
      
    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
    }
    return datos;
  }
}
